﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using QuanLyTiemThuocTay.GUI;
using System.Data.SqlClient;
using QuanLyTiemThuocTay.GiaoDien;
using QuanLyTiemThuocTay.Report;

namespace QuanLyTiemThuocTay
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btAbout_ItemClick(object sender, ItemClickEventArgs e)
        {
            new frmLienHe().ShowDialog();
        }

        private void btTroGiup_ItemClick(object sender, ItemClickEventArgs e)
        {
            MessageBox.Show("Module đang xây dựng.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btDMThuoc_ItemClick(object sender, ItemClickEventArgs e)
        {
            AddMdiChild(this, new frmThuoc());
        }


        private void btDMDVTinh_ItemClick(object sender, ItemClickEventArgs e)
        {
           AddMdiChild(this, new frmDonViTinh());
        }

        private void btDMLoaiThuoc_ItemClick(object sender, ItemClickEventArgs e)
        {
         //AddMdiChild(this, new frmDMLoaiThuoc());
        }

        private void btDMNhaCungCap_ItemClick(object sender, ItemClickEventArgs e)
        {
            AddMdiChild(this, new frmNhaCungCap());
        }

        private void btQLNDung_ItemClick(object sender, ItemClickEventArgs e)
        {
            AddMdiChild(this, new frmQLNguoiDung());
        }

        private void btDoiMK_ItemClick(object sender, ItemClickEventArgs e)
        {
            DialogResult dr = new DialogResult();
            using (frmDoiMatKhau dmk = new frmDoiMatKhau(clsHam.ndunghientai))
            {
                do
                {
                    dr = dmk.ShowDialog();

                } while (dr == DialogResult.No);
                dmk.Close();
            }
            // new frmDoiMatKhau(clsHam.ndunghientai).ShowDialog();
        }

        private void btGiaThuoc_ItemClick(object sender, ItemClickEventArgs e)
        {
            AddMdiChild(this, new frmGiaThuoc());
        }

        private void btSaoLuu_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                using (SaveFileDialog backupDialog = new SaveFileDialog { Filter = "Microsoft SQL Server Backup File (*.bak)|*.bak", FileName = String.Format("CSDL QUANLYTHUOC sao luu ngay {0} thang {1} nam {2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year) })
                {
                    if (backupDialog.ShowDialog() == DialogResult.OK)
                        using (SqlConnection con = new SqlConnection { ConnectionString = clsHam.ConnectionString() })
                        {
                            con.Open();
                            using (SqlCommand cmd = new SqlCommand(String.Format("BACKUP DATABASE QUANLYTHUOC TO DISK = '{0}'", backupDialog.FileName)) { Connection = con })
                            {
                                cmd.ExecuteNonQuery();
                                con.Close();
                            }
                            clsHam.HienThi(Text, "Sao lưu dữ liệu thành công");
                        }
                }
            }
            catch
            {
                clsHam.HienThi(Text, "Không thể sao lưu dữ liệu");
            }

        }

        private void btPhieuNhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            AddMdiChild(this, new frmPhieuNhap());
        }

        private void btHDBanLe_ItemClick(object sender, ItemClickEventArgs e)
        {
            var newfxuat = new frmPhieuXuat(false);
            newfxuat.Name = "HoaDonBanLe";
            AddMdiChild(this, newfxuat);
        }

        private void btHDBanBuon_ItemClick(object sender, ItemClickEventArgs e)
        {
            var newfxuat = new frmPhieuXuat(true);
            newfxuat.Name = "HoaDonBanBuon";
            AddMdiChild(this, newfxuat);
        }

        #region AddMdiChild
        /// <summary>
        /// Them form con vao form MDI, neu da ton tai thi hien ra
        /// Neu chua ton tai thi them moi.
        /// </summary>
        /// <param name="parentFrm">Form cha </param>
        /// <param name="childFrm">Form con</param>
        public static void AddMdiChild(Form parentFrm, Form childFrm)
        {
            Form[] childArr = parentFrm.MdiChildren;
            bool isExist = false;
            Form currentForm = null;

            foreach (Form f in childArr)
            {
                if (f.Name == childFrm.Name)
                {
                    isExist = true;
                    currentForm = f;
                    break;
                }
            }

            if (isExist == true)
            {
                currentForm.Select();
            }
            else
            {
                childFrm.MdiParent = parentFrm;
                childFrm.Show();
            }
        }

        #endregion

        private void btDSThuoc_ItemClick(object sender, ItemClickEventArgs e)
        {
            new rptDanhSachThuoc().ShowPreview();
        }

        private void btTonKho_ItemClick(object sender, ItemClickEventArgs e)
        {
            new rptTonKho().ShowPreview();
        }

        private void btDoanhThu_ItemClick(object sender, ItemClickEventArgs e)
        {
            new rptDoanhThu().ShowPreview();
        }
    }
}